home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech's Sprocket™ / SprocketGX / AppSpecific / DocWindow.cp < prev    next >
Encoding:
Text File  |  1994-10-18  |  6.9 KB  |  322 lines  |  [TEXT/MMCC]

  1. /*
  2.     File:        DocWindow.cp
  3.  
  4.     Contains:    A simple document window
  5.                 
  6.     Written by: Dave Falkenburg
  7.     
  8.     Copyright:    © 1993-94 by Dave Falkenburg, all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.     
  12.  
  13.     To Do:        Figure out if any of these methods should move to TWindow
  14.  
  15.  */
  16.  
  17. #include "DocWindow.h"
  18. #include "AppLib.h"
  19. #include <LowMem.h>        //    for LMGetCurApName()
  20. #include <ToolUtils.h>    //    for NumToString()
  21. #include <Icons.h>
  22. #include <Threads.h>
  23.  
  24. unsigned long    TDocWindow::fgUntitledTagCount = 0;
  25.  
  26. static void SpinArrowsThreadProc(TDocWindow* pWindowToSpin)
  27. {
  28.     while (true)
  29.     {
  30.         pWindowToSpin->SpinHeaderArrows();
  31.         YieldToAnyThread();
  32.     }
  33. }
  34.  
  35. static    void MyDrawGrowIcon(WindowPtr pWindow)
  36. {
  37.     Rect        aGrowRect = pWindow->portRect;
  38.     RgnHandle    aSaveClipRgn = NewRgn();
  39.     
  40.     GetClip(aSaveClipRgn);
  41.  
  42.     aGrowRect.top += kHeaderHeight;
  43.     ClipRect(&aGrowRect);
  44.     DrawGrowIcon(pWindow);
  45.     SetClip(aSaveClipRgn);
  46.     DisposeRgn(aSaveClipRgn);    
  47. }
  48.  
  49. TDocWindow::TDocWindow()
  50.  : fDocFile(nil), 
  51.      fTHPrint(nil),
  52.      fDirty(false)
  53. {    
  54.     fCurrentSpinningArrowIconID = kFirstSpinningArrowIconID;
  55.  
  56.     TDocWindow::fgUntitledTagCount++;            //    Starts out as zero but we want “Untitled-1”
  57.     this->CreateWindow();
  58.     
  59.     if (gHasThreadManager)
  60.     {
  61.         OSErr    err = NewThread(kCooperativeThread, (ThreadEntryProcPtr) SpinArrowsThreadProc, this, 0, 0, nil, &fSpinnerThreadID);
  62.         if (err != noErr)
  63.             DebugStr((StringPtr) "\pNewThread failed");
  64.     }
  65. }
  66.  
  67. TDocWindow::~TDocWindow()
  68. {
  69.     if (fDocFile)
  70.         delete fDocFile;
  71.     
  72.     if (fTHPrint)
  73.         DisposeHandle((Handle)fTHPrint);
  74.  
  75.     //    NEWS OF THE WEIRD!
  76.     //    If you pass true for recyleThread param, the heap block for the stack isn’t
  77.     //    disposed— it gets placed in the default cooperative thread pool of “premade”
  78.     //    threads.
  79.     //
  80.     //    To optimize memory usage, I might just want to go ahead and create
  81.     //    a thread pool at application startup so we can avoid moving memory around
  82.     //    so much.
  83.     
  84.     if (gHasThreadManager)
  85.     {
  86.         OSErr    err = DisposeThread(fSpinnerThreadID,nil,false);
  87.         if (err != noErr)
  88.             DebugStr((StringPtr) "\pDisposeThread failed");
  89.     }
  90. }
  91.  
  92. Boolean     TDocWindow::IsDirty(void)
  93. {
  94.     return fDirty;
  95. }
  96.  
  97. void    TDocWindow::SetDirty(Boolean  theDirtyFlag)
  98. {
  99.     fDirty = theDirtyFlag;
  100. }
  101.  
  102. WindowPtr TDocWindow::MakeNewWindow(WindowPtr pBehindWindow)
  103. {
  104.     WindowPtr    pWindow = GetNewColorOrBlackAndWhiteWindow(kDocWindowTemplateID, nil, pBehindWindow);
  105.     
  106.     if (pWindow)
  107.     {
  108.         Str63        aTitleString;
  109.         //    make the window exciting & different
  110.         //    Can’t call nudge because it assumes the window is visible!
  111.         //    Nudge(fgUntitledTagCount * kHeaderHeight,fgUntitledTagCount * kHeaderHeight);
  112.     
  113.         GetWTitle(pWindow,aTitleString);
  114.         if (*aTitleString)
  115.         {
  116.             Str31 aNumberString;
  117.             
  118.             NumToString(fgUntitledTagCount, aNumberString);
  119.             BlockMove(&aNumberString[1], &aTitleString[1+*aTitleString], *aNumberString);
  120.             *aTitleString += *aNumberString;
  121.         }
  122.         SetWTitle(pWindow, aTitleString);
  123.     }
  124.     return pWindow;
  125. }
  126.  
  127.  
  128. void TDocWindow::Activate(Boolean /* theIsActivating */)
  129. {
  130.     MyDrawGrowIcon(fWindow);
  131. }
  132.  
  133. void TDocWindow::AdjustCursor(EventRecord * /* pEvent */)
  134. {
  135. }
  136.  
  137. void TDocWindow::Draw(void)
  138. {
  139.     EraseRect(&fWindow->portRect);
  140.  
  141.     MoveTo(0,kHeaderHeight-3); 
  142.     LineTo(fWindow->portRect.right,kHeaderHeight-3);
  143.     MoveTo(0,kHeaderHeight-1); 
  144.     LineTo(fWindow->portRect.right,kHeaderHeight-1);
  145.         
  146.     MyDrawGrowIcon(fWindow);
  147. }
  148.  
  149. void TDocWindow::Click(EventRecord * /* pEvent */)
  150. {
  151.     this->Select();
  152. }
  153.  
  154. void TDocWindow::SetupMenus(void)
  155. {
  156.     TWindow::SetupMenus();
  157.     if (IsDirty())
  158.         EnableMenuItem(mFile, iSave, true);
  159.     EnableMenuItem(mFile, iSaveAs, true);
  160.     EnableMenuItem(mFile, iPageSetup, true);
  161.     EnableMenuItem(mFile, iCustomPageSetup, true);
  162.     EnableMenuItem(mFile, iPrint, true);
  163.     EnableMenuItem(mFile, iPrintOneCopy, true);
  164. }
  165.  
  166. void TDocWindow::AdjustForNewWindowSize(Rect* pOldSize, Rect* /* pNewSize*/)
  167. {
  168.     Rect    aScrollbarRect;
  169.  
  170.     //    Invalidate the old vertical scroll bar
  171.     aScrollbarRect = *pOldSize;
  172.     aScrollbarRect.left = aScrollbarRect.right - kScrollbarWidth;
  173.     InvalRect(&aScrollbarRect);
  174.  
  175.     //    Invalidate the old horizontal scroll bar
  176.     aScrollbarRect.left = pOldSize->left;
  177.     aScrollbarRect.top = pOldSize->bottom - kScrollbarWidth;
  178.     InvalRect(&aScrollbarRect);
  179.  
  180.     MyDrawGrowIcon(fWindow);
  181. }
  182.  
  183. Boolean TDocWindow::SaveDocument(void)
  184. {
  185.     return true;
  186. }
  187.  
  188. Boolean TDocWindow::SaveDocumentAs(void)
  189. {
  190.     return true;
  191. }
  192.  
  193. void  TDocWindow::HandleMenuCommand(short theMenuID, short theMenuItem)
  194. {
  195.     Boolean  aHandled = true;
  196.     switch(theMenuID)
  197.     {
  198.         case mFile :
  199.             switch(theMenuItem)
  200.             {
  201.                 default :        aHandled = false;                break;
  202.             }
  203.             break;
  204.             
  205.         default :        aHandled = false;                break;
  206.     }
  207.     if ( ! aHandled)
  208.         TWindow::HandleMenuCommand(theMenuID, theMenuItem);
  209. }
  210.  
  211. void TDocWindow::PageSetupDlg(Boolean /*theCustomFlag*/)
  212. {
  213.     if (fTHPrint == nil)
  214.         fTHPrint = (THPrint)NewHandleClear(sizeof(TPrint));
  215.     PrOpen();
  216.     if ( ! PrValidate(fTHPrint))
  217.         PrintDefault(fTHPrint);
  218.     if (PrStlDialog(fTHPrint))
  219.         ;
  220.     PrClose();
  221. }
  222.  
  223. void TDocWindow::PrintDlg(Boolean /*theDialogFlag*/)
  224. {
  225.     if (fTHPrint == nil)
  226.         fTHPrint = (THPrint)NewHandleClear(sizeof(TPrint));
  227.     PrOpen();
  228.     if ( ! PrValidate(fTHPrint))
  229.         PrintDefault(fTHPrint);
  230.     if (PrJobDialog(fTHPrint))
  231.         SysBeep(1);    // <= print now
  232.     PrClose();
  233. }
  234.  
  235. Boolean TDocWindow::Close(void)
  236. {
  237.     if (this->IsDirty())
  238.     {
  239.         StandardCloseResult    aResult;
  240.         Str255                aDocTitle;
  241.         
  242.         GetWTitle(this->fWindow, aDocTitle);
  243.         aResult = StandardCloseDocument(LMGetCurApName(), aDocTitle, false, false);
  244.     
  245.         if (aResult == kCancelSaveDocument)
  246.             return false;
  247.         if (aResult == kSaveDocument)
  248.             if (SaveDocument() == false)
  249.                 return false;
  250.     }
  251.     return TWindow::Close();
  252. }
  253.  
  254. void  TDocWindow::SetDocFile(FSSpec* pFSSpec)
  255. {
  256.     fDocFile = new TDocFile;
  257.     if (fDocFile)
  258.     {
  259.         fDocFile->SetFSSpec(pFSSpec);
  260.         fDocFile->SetForkAvail(true, true);
  261.         if (pFSSpec)
  262.             SetWTitle(fWindow, pFSSpec->name);
  263.     }
  264. }
  265.  
  266. OSErr TDocWindow::DragEnterWindow(DragReference theDrag)
  267. {
  268.     return this->DragInWindow(theDrag);
  269. }
  270.  
  271. OSErr TDocWindow::DragInWindow(DragReference theDrag)
  272. {
  273.     RgnHandle    aHiliteRgn = NewRgn();
  274.     Point        aMouseLoc;
  275.     Point        aPinnedMouseLoc;
  276.     
  277.     SetRectRgn( aHiliteRgn,
  278.                 fWindow->portRect.left,
  279.                 fWindow->portRect.top + kHeaderHeight,
  280.                 fWindow->portRect.right-kScrollbarWidth,
  281.                 fWindow->portRect.bottom-kScrollbarWidth);
  282.  
  283.     (void) GetDragMouse(theDrag,&aMouseLoc,&aPinnedMouseLoc);
  284.     GlobalToLocal(&aMouseLoc);
  285.     
  286.     if (PtInRgn(aMouseLoc,aHiliteRgn))
  287.         ShowDragHilite(theDrag,aHiliteRgn,true);
  288.     else
  289.         HideDragHilite(theDrag);
  290.         
  291.     DisposeRgn(aHiliteRgn);    
  292.     return noErr;
  293. }
  294.  
  295. OSErr TDocWindow::DragLeaveWindow(DragReference theDrag)
  296. {
  297.     HideDragHilite(theDrag);
  298.     return noErr;
  299. }
  300.  
  301. OSErr TDocWindow::HandleDrop(DragReference /* theDrag */)
  302. {
  303.     return noErr;
  304. }
  305.  
  306. void TDocWindow::SpinHeaderArrows()
  307. {
  308.     CSavePort  aSavePort(fWindow);
  309.     Rect    aSpinRect;
  310.     
  311.     fCurrentSpinningArrowIconID++;
  312.     
  313.     if (fCurrentSpinningArrowIconID > kLastSpinningArrowIconID)
  314.         fCurrentSpinningArrowIconID = kFirstSpinningArrowIconID;
  315.     
  316.     aSpinRect.top = fWindow->portRect.top;
  317.     aSpinRect.left = fWindow->portRect.left+4;
  318.     aSpinRect.bottom = aSpinRect.top + 16;
  319.     aSpinRect.right = aSpinRect.left + 16;
  320.     (void) PlotIconID(&aSpinRect,atNone,ttNone,fCurrentSpinningArrowIconID);
  321. }
  322.